import 'package:flutter/material.dart';
import 'package:jd_shop/routers/AppRoutes.dart';
import 'package:jd_shop/utils/ToastMsg.dart';
import 'package:provider/provider.dart';

import '../../event/ProductContentEvent.dart';
import '../../model/LoginUserBean.dart';
import '../../model/UserCenterBean.dart';
import '../../net/UsermsService.dart';
import '../../provider/AppProvider.dart';
import '../../services/ScreenAdaper.dart';
import '../../utils/Storage.dart';

class MyPage extends StatefulWidget {
  const MyPage({super.key});

  @override
  State<StatefulWidget> createState() {
    return _MyPageState();
  }
}

class _MyPageState extends State<MyPage> with AutomaticKeepAliveClientMixin {
  var isLoginUser = false;
  LoginUserBean? loginUserBean;
  UserCenterBean? userCenterBean;
  final ScrollController _scrollController = ScrollController();

  @override
  void initState() {
    super.initState();
    _getUserState();
    eventBus.on<UserLoginEvent>().listen((event) {
      _getUserState();
    });
  }

  _getUserState() async {
    final newUserBean = await Storage.getLoginUser();
    final newIsLoginUser = await Storage.isLoginUser();
    UserCenterBean? newUserCenterBean;
    if (newIsLoginUser) {
      newUserCenterBean = await UsermsService.getUserDetail();
    }
    setState(() {
      loginUserBean = newUserBean;
      isLoginUser = newIsLoginUser;
      userCenterBean = newUserCenterBean;
    });
  }

  @override
  Widget build(BuildContext context) {
    ScreenAdapter.init(context);
    return MediaQuery.removePadding(
        removeTop: true,
        context: context,
        child: Column(
          children: [
            Container(
              height: ScreenAdapter.height(70),
              color: Colors.blue,
              alignment: Alignment.bottomCenter,
              child: Container(
                height: ScreenAdapter.height(40),
                width: ScreenAdapter.getScreenWidth(),
                alignment: Alignment.center,
                child: Stack(children: [
                  Align(
                      alignment: Alignment.center,
                      child: Text("我的",
                          textAlign: TextAlign.end,
                          style: TextStyle(
                              fontSize: ScreenAdapter.fontSize(16),
                              fontWeight: FontWeight.bold,
                              color: Colors.white))),
                  Align(
                      alignment: Alignment.centerRight,
                      child: InkWell(
                        child: Container(
                          width: ScreenAdapter.width(50),
                          height: ScreenAdapter.width(50),
                          child: Icon(
                            Icons.settings,
                            color: Colors.white,
                            size: ScreenAdapter.width(25),
                          ),
                        ),
                        onTap: () {
                          Navigator.pushNamed(context, AppRoutes.SETTING);
                        },
                      ))
                ]),
              ),
            ),
            SizedBox(
                width: ScreenAdapter.getScreenWidth(),
                height:
                    ScreenAdapter.getScreenHeight() - ScreenAdapter.height(120),
                child: ListView(
                  controller: _scrollController,
                  children: [
                    Row(
                      children: [
                        InkWell(
                          child: Container(
                            width: ScreenAdapter.width(50),
                            height: ScreenAdapter.width(50),
                            margin: EdgeInsets.only(
                                left: ScreenAdapter.width(20),
                                top: ScreenAdapter.height(20)),
                            child: ClipRRect(
                              borderRadius: BorderRadius.circular(250.0),
                              // 你可以根据需要调整圆角半径
                              child: userCenterBean == null
                                  ? Image.asset(
                                      "images/mv.webp",
                                      fit: BoxFit.cover,
                                    )
                                  : Image.network(
                                      userCenterBean!.userAvatar.toString(),
                                      fit: BoxFit.cover),
                            ),
                          ),
                          onTap: () {
                            if (!isLoginUser) {
                              Navigator.pushNamed(context, AppRoutes.LOGIN);
                            } else {
                              Navigator.pushNamed(context, AppRoutes.SETTING);
                            }
                          },
                        ),
                        InkWell(
                          child: Container(
                            margin: EdgeInsets.only(
                                left: ScreenAdapter.width(10),
                                top: ScreenAdapter.height(20)),
                            child: userCenterBean == null
                                ? Text("请登录...",
                                    style: TextStyle(
                                        fontSize: ScreenAdapter.fontSize(16),
                                        fontWeight: FontWeight.bold))
                                : Text(
                                    userCenterBean!.userNickname.toString(),
                                    style: TextStyle(
                                        fontSize: ScreenAdapter.fontSize(14),
                                        fontWeight: FontWeight.bold),
                                  ),
                          ),
                          onTap: () {
                            if (!isLoginUser) {
                              Navigator.pushNamed(context, AppRoutes.LOGIN);
                            } else {
                              Navigator.pushNamed(context, AppRoutes.SETTING);
                            }
                          },
                        )
                      ],
                    ),
                    SizedBox(
                      height: ScreenAdapter.height(20),
                    ),
                    Container(
                        margin: const EdgeInsets.all(10),
                        padding: const EdgeInsets.all(10),
                        decoration: BoxDecoration(
                          color: Colors.white,
                          borderRadius: BorderRadius.circular(10.0),
                        ),
                        child: Column(children: [
                          Stack(children: [
                            SizedBox(
                              width: ScreenAdapter.getScreenWidth() -
                                  ScreenAdapter.width(21),
                              child: Text(
                                "我的订单",
                                style: TextStyle(
                                    fontSize: ScreenAdapter.fontSize(16),
                                    fontWeight: FontWeight.bold),
                              ),
                            ),
                            Positioned(
                              right: ScreenAdapter.width(20),
                              top: 0,
                              bottom: 0,
                              child: InkWell(
                                child: Text(
                                  "查看全部",
                                  style: TextStyle(
                                      color: Colors.grey.shade700,
                                      fontSize: ScreenAdapter.fontSize(14)),
                                ),
                                onTap: () {
                                  ToastMsg.show("查看全部");
                                  // Navigator.pushNamed(context, AppRoutes.ORDER);
                                },
                              ),
                            ),
                            Positioned(
                              right: 0,
                              top: 0,
                              bottom: 0,
                              child: Icon(
                                Icons.arrow_forward_ios,
                                color: Colors.grey,
                                size: ScreenAdapter.width(15),
                              ),
                            ),
                          ]),
                          SizedBox(
                            height: ScreenAdapter.width(15),
                          ),
                          Row(
                            children: [
                              Expanded(
                                flex: 1,
                                child: InkWell(
                                  child: Column(
                                    children: [
                                      Image.asset(
                                        "images/yl.png",
                                        fit: BoxFit.cover,
                                        width: ScreenAdapter.width(22),
                                        height: ScreenAdapter.width(22),
                                      ),
                                      SizedBox(
                                        height: ScreenAdapter.height(5),
                                      ),
                                      Text(
                                        "待付款",
                                        style: TextStyle(
                                            fontSize:
                                                ScreenAdapter.fontSize(12)),
                                      )
                                    ],
                                  ),
                                  onTap: () {
                                    ToastMsg.show("待付款");
                                    // Navigator.pushNamed(context, AppRoutes.ORDER);
                                  },
                                ),
                              ),
                              Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    child: Column(
                                      children: [
                                        Image.asset(
                                          "images/yl.png",
                                          fit: BoxFit.cover,
                                          width: ScreenAdapter.width(22),
                                          height: ScreenAdapter.width(22),
                                        ),
                                        SizedBox(
                                          height: ScreenAdapter.height(5),
                                        ),
                                        Text(
                                          "待发货",
                                          style: TextStyle(
                                              fontSize:
                                                  ScreenAdapter.fontSize(12)),
                                        )
                                      ],
                                    ),
                                    onTap: () {
                                      ToastMsg.show("待发货");
                                      // Navigator.pushNamed(context, AppRoutes.ORDER);
                                    },
                                  )),
                              Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    child: Column(
                                      children: [
                                        Image.asset(
                                          "images/yl.png",
                                          fit: BoxFit.cover,
                                          width: ScreenAdapter.width(22),
                                          height: ScreenAdapter.width(22),
                                        ),
                                        SizedBox(
                                          height: ScreenAdapter.height(5),
                                        ),
                                        Text(
                                          "待收货",
                                          style: TextStyle(
                                              fontSize:
                                                  ScreenAdapter.fontSize(12)),
                                        )
                                      ],
                                    ),
                                    onTap: () {
                                      ToastMsg.show("待收货");
                                      // Navigator.pushNamed(context, AppRoutes.ORDER);
                                    },
                                  )),
                              Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    child: Column(
                                      children: [
                                        Image.asset(
                                          "images/yl.png",
                                          fit: BoxFit.cover,
                                          width: ScreenAdapter.width(22),
                                          height: ScreenAdapter.width(22),
                                        ),
                                        SizedBox(
                                          height: ScreenAdapter.height(5),
                                        ),
                                        Text(
                                          "待评价",
                                          style: TextStyle(
                                              fontSize:
                                                  ScreenAdapter.fontSize(12)),
                                        )
                                      ],
                                    ),
                                    onTap: () {
                                      ToastMsg.show("待评价");
                                      // Navigator.pushNamed(context, AppRoutes.ORDER);
                                    },
                                  )),
                              Expanded(
                                  flex: 1,
                                  child: InkWell(
                                    child: Column(
                                      children: [
                                        Image.asset(
                                          "images/yl.png",
                                          fit: BoxFit.cover,
                                          width: ScreenAdapter.width(22),
                                          height: ScreenAdapter.width(22),
                                        ),
                                        SizedBox(
                                          height: ScreenAdapter.height(5),
                                        ),
                                        Text(
                                          "退货/售后",
                                          style: TextStyle(
                                              fontSize:
                                                  ScreenAdapter.fontSize(12)),
                                        )
                                      ],
                                    ),
                                    onTap: () {
                                      ToastMsg.show("退货/售后");
                                      // Navigator.pushNamed(context, AppRoutes.ORDER);
                                    },
                                  )),
                            ],
                          )
                        ])),
                  ],
                )),
          ],
        ));
  }

  @override
  bool get wantKeepAlive => false;
}
